<template>
  <div>
    <el-dialog title="修改密码" :visible.sync="visible">
      <el-form :model="tableForm" ref="tableForm" :rules="tableRules">
        <el-form-item
          label="原密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="tableForm.password"
            placeholder="请输入原密码"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="新密码"
          :label-width="formLabelWidth"
          prop="password1"
        >
          <el-input
            v-model="tableForm.password1"
            placeholder="请输入密码"
            :key="passwordType"
            :type="passwordType"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="确认密码"
          :label-width="formLabelWidth"
          prop="password2"
        >
          <el-input
            v-model="tableForm.password2"
            placeholder="请再输入一遍密码"
            :key="passwordType"
            ref="password"
            :type="passwordType"
          ></el-input>
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetHandler('tableForm')">重 置</el-button>
        <el-button type="primary" @click="submit('tableForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { setPassword } from "@/api/user";
export default {
  name: "TmsVueSetPassword",

  data() {
    const validatePassword = (rule, value, callback) => {
      if (this.tableForm.password2 == "") {
        callback(new Error("新密码必须填写！"));
      } else if (this.tableForm.password1 != this.tableForm.password2) {
        callback(new Error("两次输入的密码不对！"));
      } else if (this.tableForm.password1.length < 6) {
        callback(new Error("密码位数不得小于6！"));
      } else {
        callback();
      }
    };
    return {
      tableForm: {
        password: "",
        password1: "",
        password2: "",
      },
      tableRules: {
        password: [
          { required: true, message: "原密码必须填写！", trigger: "blur" },
        ],
        password1: [
          { required: true, message: "新密码必须填写！", trigger: "blur" },
        ],
        password2: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      formLabelWidth: "80px",
      passwordType: "password",
    };
  },

  methods: {
    resetHandler(tableForm) {
      this.$refs[tableForm].resetFields();
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    submit(tableForm) {
      this.$refs[tableForm].validate((valid) => {
        if (valid) {
          //这里要验证旧密码是否正确，正确才能修改
          const oldPassword = this.tableForm.password;
          const newPassword = this.tableForm.password1;
          const data = { oldPassword, newPassword };
          setPassword(data).then((res) => {
            this.$message({
              message: "密码修改成功",
              type: "success",
            });
            //这里要退出登录
            this.tableForm = {
              password: "",
              password1: "",
              password2: "",
            };
            const _this = this;
            setTimeout(() => {
              _this.$store.dispatch("user/logout").then((res) => {
                _this.$router.push(`/login?redirect=${_this.$route.fullPath}`);
              });
            }, 1000);
          });
        } else {
          console.log("error submit!!");
        }
      });
    },
  },

  props: {
    dialogTableVisible: false,
  },
  computed: {
    visible: {
      get() {
        return this.dialogTableVisible;
      },
      set(value) {
        this.$emit("update:dialogTableVisible", {
          type: "setPassword",
          val: value,
        });
      },
    },
  },
};
</script>

<style  lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
}
</style>